#include "hotkeytest.h"
#include <QAction>
#include <QTimer>

HotKeyTest::HotKeyTest(QObject* parent)
	: QObject(parent)
{
}
HotKeyTest::~HotKeyTest()
{
}

void HotKeyTest::initTestCase()
{
	// 注册全局热键 Ctrl + Alt + 1
	auto res1 = HotKey::registerHotKey(nullptr, "Ctrl+Alt+1");
	QVERIFY(res1);// 注册成功才继续

	auto res2 = HotKey::registerHotKey(nullptr, "Ctrl+Alt+2");
	QVERIFY(res2);

	auto res3 = HotKey::registerHotKey(nullptr, "Ctrl+Alt+3");
	QVERIFY(res3);

	// 创建并安装全局事件过滤器
	m_filter = new GlobalKeyFilter();
	m_filter->install();

	connect(m_filter, &GlobalKeyFilter::hotKeyStriked, this, [=](int id) {
		if (id == res1)
		{
			LOG_INFO("HotKeyTest::tstHotKey", " 快捷键被按下 : ctrl + alt + 1");
		}
		if (id == res2)
		{
			// HotKey::unregisterHotKey(nullptr, id);
			// HotKey::unregisterHotKey(nullptr, 195);
			LOG_INFO("HotKeyTest::tstHotKey", " 快捷键被按下 : ctrl + alt + 2, 快捷键2 与 快捷键1 已注销");
		}
		if (id == res3)
		{
			LOG_INFO("HotKeyTest::tstHotKey", " 快捷键被按下 : ctrl + alt + 3");
		}
	});
}

void HotKeyTest::tstHotKey()
{
	// 提示：全局热键只能手动触发或用系统 API 模拟
	LOG_INFO("HotKeyTest::tstHotKey", "请按 Ctrl + Alt + 1/2/3 来触发热键");

	// 等待 10 秒，让你有时间手动按
	QTest::qWait(10000);
}

void HotKeyTest::cleanupTestCase()
{
	if (m_filter)
	{
		delete m_filter;
		m_filter = nullptr;
	}

	// HotKey::unregisterHotKey(nullptr, 195);
}


int main(int argc, char** argv)
{
#ifdef Q_OS_WIN
	SetConsoleOutputCP(CP_UTF8);
	SetConsoleCP(CP_UTF8);
#endif

	QApplication app(argc, argv);
	HotKeyTest tc;
	return QTest::qExec(&tc, argc, argv);
}